<template>
  <div class="dictionaryData h100">
    <object id="LODOP_OB" classid="clsid:2105C259-1E0C-4534-8141-A753534CB4CA" width=0 height=0> 
      <embed id="LODOP_EM" type="application/x-print-lodop" width=0 height=0></embed>
    </object>
    <div class="PageContent">
      <el-form
        :model="searchFormData"
        :inline="true"
        ref="propertyRuleForm"
        class="demo-ruleForm"
        @submit.native.prevent
      >
        <div class="PageItem margintop20">
      <el-row class="paddingleft2em" >
        <el-form-item label="　资产条码" style="margin-bottom: 0;">
          <el-input
            v-model="searchFormData.assetsNo"
            ref="assetsNo"
            @keyup.enter.native="searchTable"
            :maxlength="20"
            placeholder="请扫描资产条码"
          >
          </el-input>
        </el-form-item>
        <el-button type="primary"   v-if="btnRight.btnPropertyScan" @click="scanData()">扫描</el-button>
        <el-button type="primary"  v-if="btnRight.btnPropertyExport" @click="exportData()">导出</el-button>
        <el-button type="primary"  v-if="btnRight.btnPropertyPrint" @click="printCode()">打印资产条码</el-button>
        <el-button type="primary"  v-if="btnRight.btnPropertyDelete" @click="deleteRow()">删除</el-button>
        <el-button type="primary"  v-if="btnRight.btnPropertyEdit" @click="editRow()">修改</el-button>
        <el-button type="primary"  v-if="btnRight.btnPropertyImport" @click="$refs.file.click()">导入</el-button>
        <input type="file" style="display:none" ref="file" @change="fileChange" />
      </el-row>
        </div>
      </el-form>
      <el-row :gutter="5" class="PageItem"  style="height: 30px;"> </el-row>
      <el-table
        ref="singleCrematorTable" :header-cell-style="{background:'rgb(246,247,251)',color:'#606266'}"
        :data="propertyTableData"
        stripe
        border
        @row-click="handleRowClick"
        @select="select" @select-all="selectAll"
        style="width: 100%;height: calc(100% - 110px);overflow-y: scroll">
        <el-table-column
          type="selection"
          width="55">
        </el-table-column>
        <el-table-column
          property="assetsNo"
          label="资产编码"
          align = 'center'
          width="150">
        </el-table-column>
        <el-table-column
          property="assetsName"
          label="资产名称"
          align = 'center'
          width="100">
        </el-table-column>
        <el-table-column
          property="getWay"
          label="取得方式"
          align = 'center'
          width="100">
        </el-table-column>
        <el-table-column
          property="enableDate"
          label="取得日期"
          align = 'center'
          width="100">
        </el-table-column>
        <el-table-column
          property="assetsCardNo"
          label="卡片序列号"
          align = 'center'
          width="150">
        </el-table-column>
        <el-table-column
          property="assetsLife"
          label="预计使用年限"
          align = 'center'
          width="120">
        </el-table-column>
        <el-table-column
          property="scrapDate"
          label="到期日期"
          align = 'center'
          width="100">
        </el-table-column>
        <el-table-column
          property="assetsSort"
          label="资产分类"
          align = 'center'
          width="100">
        </el-table-column>
        <el-table-column
          property="specificationNo"
          label="规格型号"
          align = 'center'
          width="100">
        </el-table-column>
        <el-table-column
          property="promoteSum"
          label="累计折旧/摊销"
          align = 'center'
          width="150">
        </el-table-column>
        <el-table-column
          property="originalValue"
          align = 'center'
          label="原值"
          width="50">
        </el-table-column>
        <el-table-column
          property="netValue"
          align = 'center'
          label="净值"
          width="50">
        </el-table-column>
        <el-table-column
          property="extractNum"
          align = 'center'
          label="已计提期间"
          width="150">
        </el-table-column>
        <el-table-column
          property="storageLoc"
          align = 'center'
          label="存放地点"
          width="150">
        </el-table-column>
      </el-table>
      <p style="text-align: right;margin-top: 20px;">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pager.currentPage"
          :page-size="pager.pageSize"
          background
          layout="prev, pager, next, jumper"
          :total="pager.total">
        </el-pagination>
      </p>
    </div>
  </div>
</template>
<script>
  import * as API from '@/api/systemApplication'
  import download from "@/api/download"
  import {getLodop} from '@/utils/lodopFuncs'

  export default {
    name: "propertyMng",
    data(){
      return{
        defaultProps: {
          children: 'children',
          label: 'label'
        },
        currentRow: null,
        tableSelection:[],//表格选中数据
        saveFlag:false, //保存标志
        onPresscTime:0,  //  阻止短时间内连续点击
        dialogPropertyVisible:false,  //项目分类维护对话框
        searchFormData:{       //保存资产信息
          "assetsNo":"",//资产编码
        },
        furnaceType:[],
        disableState: [
          {label: "有效可用", value: 0}, {label: "无效禁用", value: 1}, {label: "维修", value: 3}
        ],
        propertyTableData:[],
        pager:{
          "pageSize": 15,
          "total": 1,
          "currentPage": 1
        },
        ruleItems:[],//收费规则
        btnRight: {
          btnPropertyScan: false, //扫描
          btnPropertyExport: false,//导出
          btnPropertyPrint: false,//打印资产条码
          btnPropertyDelete: false,//删除
          btnPropertyEdit: false,//修改资产
          btnPropertyImport: false,//导入
        }
      }
    },
    mounted(){
      //载入设备列表
      // this.loadPropertyList();
      this.scanData();
      //获得菜单按钮
      this.btnRight = this.$route.meta.buttons;
      //遍历属性赋值将true与false字符串转换成布尔型的值
      for (let key of Object.keys(this.btnRight)) {
        this.btnRight[key]=(this.btnRight[key]|| this.btnRight[key]=="true")?true:false;
      }
    },
    methods: {
     //加载固定资产信息
      loadPropertyList(){
        let postData=this.searchFormData;
        API['loadFixedAssetsItems'](postData).then(response=>{
          let { data }=response;
          if (!data) {
            this.$message('数据查询失败')
            return
          }else if(data.list!=null && data.pager!=null){
            this.propertyTableData = data.list;
            this.pager = data.pager
          }

        }).catch(error => {
          this.$message(error)
        })
      },changeSort(val){
        val = val.replace(/(^\s*)|(\s*$)/g, "")
        if(!val) {
          this.saveFormData.sort= "";
          return
        }
        let reg = /[^\d]/g
        // 只能是数字和小数点，不能是其他输入
        val = val.replace(reg, "")
        if (val.length>10)
          val=val.slice(0,10)
        this.saveFormData.sort = val;
      },
      scanData() {
        let postData=this.searchFormData;
        if(postData.assetsNo==''||postData.assetsNo==""){
          postData.assetsNo=null;
        }
        API['loadFixedAssetsItems'](postData).then(response=>{
          let { data }=response;
          if (!data) {
            this.$message('数据查询失败')
            return
          }
          this.propertyTableData = data.list;
          this.pager = data.pager
        }).catch(error => {
          this.$message(error)
        })

      },//导出数据
      exportData(){
        let postData = {
          "assetsNo":this.searchFormData.assetsNo
        }
        download.exportExcel(API["AssetsQueryEXPEXC"],postData,"固定资产查询_"+download.getDateTime()+".xlsx");
      },deleteRow(){
        let this_delete=this;
        if( this.tableSelection.length<1){
          this.$message("请选中一条记录进行操作");
        }else{
          //开始删除
          this.$confirm('删除信息, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            let ids=new Array();
            ids.push(this_delete.tableSelection[0].id);
            let postData={
              "fixedIds":ids
            }
            API['delFixedAssetsByIds'](postData).then(response=>{
              let { code }=response;
              if (!code ||code==0) {
                this_delete.$message('删除失败')
                return
              }else{
                this_delete.$message('删除成功')
                //重新查询
                this_delete.scanData();

              }
            }).catch(error => {
              this_delete.$message(error)
            })
          }).catch(() => {
          });
        }
      },editRow(){
        let this_edit=this;
        if( this_edit.tableSelection.length<1){
          this_edit.$message("请选中一条记录进行操作");
        }else{
          let assetsNo=this_edit.tableSelection[0].assetsNo;
          this_edit.$router.push({ name: 'home.property.propertyEdit', params: {  assetsNo }});

        }

      },importRow(){

      },//文件导入
      fileChange(e) {
        console.log("文件变化了");
        // console.log(e.target.files[0]);
        e.target.files[0];
        let formData = new FormData();
        formData.append("importExcelFile", e.target.files[0]);

        API['importExcelAssetsInfo'](formData).then(response=>{
          let { data }=response;
       console.log(response,333)

        })
        // this.$http({
        //   method: "post",
        //   url: "/api/assets/import-excel-assets-info",
        //   data: formData
        // }).then(res => {
        //   if (!res.code) {
        //     this.$message({
        //       message: res.message,
        //       type: "warning"
        //     });
        //     this.$refs.file.value = "";
        //
        //     return;
        //   }
          // this.$refs.file.value = "";
          // this.$message({
          //   message: res.message,
          //   type: "success"
          // });
          // //重新查询列表
          // this.scanData();

      },
      //打印二维码
      printCode(){
        let this_print=this;
        if( this.tableSelection.length<1){
          this.$message("请选中一条记录进行打印");
        }else{
          //开始打印
          let postData={
            "assetsNo":this_print.tableSelection[0].assetsNo
          }
          API['loadFixedAssetsInfoByAssetsNo'](postData).then(response=>{
            let { data }=response;
            if (!data) {
              this.$message('数据查询失败')
              return
            }else{
              //打印二维码
              this_print.printCardNo(data);
            }
          }).catch(error => {
            this.$message(error)
          })
        }

      },
      //打印服务协议单
      printCardNo(data){
        let this_print=this;
        let LODOP =  getLodop();
        if (LODOP == null || !LODOP) {
          this_print.$message.alert('系统提示', '您的浏览器不支持打印控件，请用IE重试或安装打印控件后重试');
          return;
        }
        let assetsCardNo=data.assetsCardNo==null?"":data.assetsCardNo;
        let assetsName=data.assetsName==null?"":data.assetsName;
        let specificationNo=data.specificationNo==null?"":data.specificationNo;
        //纸张
        //LODOP.SET_PRINT_PAGESIZE(1, 0, 0, "A4");
        //设置字体
        LODOP.SET_PRINT_STYLE("FontSize",11);
        LODOP.ADD_PRINT_BARCODE(35, 275,70, 70, "QRCode",assetsCardNo);
        LODOP.ADD_PRINT_TEXT(30,50,270,25,"资产名称："+assetsName);
        LODOP.ADD_PRINT_HTM(60,50,270,25,"规格型号："+specificationNo);
        LODOP.ADD_PRINT_TEXT(90,50,270,25,"资产条码："+assetsCardNo);

        //调用打印控件进行测试
        //打印边框
        //直接打印
        //打印预览
        LODOP.PREVIEW();
        //LODOP.PRINT();
        //LODOP.PRINT_DESIGN();
      },
      formatterRuleItems: function(row, column, cellValue) {
        let data=cellValue;
        if( data && data.length && data.length>0){
          let itemStr="";
          for(var i=0;i<data.length;i++){
            itemStr+=data[i].ruleDescribe;
          }
          return itemStr;
        }else{
          return "暂无";
        }

      },
      closeDialog(){
        // this.saveCremator={};
        this.dialogPropertyVisible=false;
      }
      , handleSizeChange: function (size) {
        this.pager.pagesize = size;
      },
      handleCurrentChange: function(currentPage){
        this.pager.currentPage = currentPage;
        let data = this.$refs.tree.getCheckedNodes()
        if (data && data.length) {
          this.loadGoodsServiceListWithTypeData(data[0].id) //点击第几页
        }
      },
    /*  //tree单选
      handleCheck(data,checked, node){
        this.$refs.crematorTree.setCheckedNodes([data]);
        node = this.$refs.crematorTree.getCheckedNodes();
        console.log(node[0].furnaceCode);
      },*/
      //点击行触发，选中或不选中复选框
      handleRowClick(row, column, event) {
        if (this.$refs.singleCrematorTable.selection.length>0){
          this.$refs.singleCrematorTable.toggleRowSelection(this.tableSelection[0],false);
        }
        this.$refs.singleCrematorTable.toggleRowSelection(row);
        this.tableSelection = [row]
      },
      select(selection, row) {
        // 选择项大于1时
        if (selection.length > 1) {
          let del_row = selection.shift();
          // console.log('把数组的第一个元素从其中删除后', selection);
          this.$refs.singleCrematorTable.toggleRowSelection(del_row, false); // 用于多选表格，切换某一行的选中状态，如果使用了第二个参数，则是设置这一行选中与否（selected 为 true 则选中）
        }
        this.tableSelection = selection
      },
      selectAll(selection) {
        //单选，去掉为多选
        if (selection.length >1) {
          selection.length = 1;
        }
        this.tableSelection = selection
      },
      //验证整数
      onlySortNumber(){
        // 先把非数字的都替换掉，除了数字和.
        let obj=this.saveFormData.sort;
        obj=Number.parseInt(obj);
        this.saveFormData.sort=obj;
      },searchTable(){
        this.loadPropertyList();

      },searchData(){
        this.loadPropertyList();
      }

    },
    watch:{
      "dialogPropertyVisible"(){
        if (this.dialogPropertyVisible==false){
          this.$nextTick(() => {
            this.$refs.crematorDialog.resetFields();
          })
        }

      },
      '$route': 'scanData'
    }
  }
</script>
<style scoped>

</style>
